var util = require('./utils/util.js')
let that
App({
  globalData: {
    url: 'https://restapi.cnbbx.com',
    method: 'post'
  },

  onLaunch: function () {
    that = this
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo'] === true) {
          that.getUserInfo()
        } else {
          wx.showToast({
            title: '需要授权登陆后才能使用',
            duration: 1500,
            mask: true,
            icon: 'loading',
          })
        }
      }
    })
  },

  getUserInfo: function () {
    wx.getUserInfo({
      success: res => {
        let openid = wx.getStorageSync("openId")
        if (!openid) {
          wx.login({
            success: loginres => {
              let code = loginres.code
              util.addLogs("获取code：" + code)
              if (code) {
                that.ajax({
                  "url": "/wechat",
                  "method": "get",
                  "data": {
                    code: code
                  },
                  "success": function (json) {
                    res.userInfo.openId = json.items[0].openid
                    if (that.userInfoReadyCallback) {
                      that.userInfoReadyCallback(res)
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  },

  /**
   * ajax 封装
   */
  ajax: function (model) {
    wx.showLoading({
      title: '通信中...',
    })
    //拼接url
    model.url = that.globalData.url + model.url
    if (model.method == undefined) model.method = that.globalData.method
    //get参数拼接
    if (model.method == "get" && model.data !== undefined) {
      let i = 0
      for (let k in model.data) {
        if (model.data[k].toString() !== '') {
          model.url = model.url + (i == 0 ? "?" : "&") + k + "=" + model.data[k]
          i++
        }
      }
      model.data = ''
    }
    //返回Promise对象
    return new Promise(
      function (resolve) {
        wx.request({
          method: model.method,
          url: model.url,
          data: model.data,
          success: (res) => {
            wx.hideLoading()
            if (res.statusCode == 200) {
              resolve(res.data)
              if (typeof model.success === "function") model.success(res.data)
            } else {
              //错误信息处理
              wx.showToast({
                title: '请稍后重试',
                duration: 500,
                mask: true,
                icon: 'loading',
              })
            }
          },
          fail: (error) => {
            wx.hideLoading()
            //错误信息处理
            wx.showToast({
              title: '请求超时',
              duration: 500,
              mask: true,
              icon: 'loading',
            })
          }
        })
      }
    )
  },

  reg: function (userInfo) {
    let isReg = wx.getStorageSync('isReg') || false
    if (!isReg) {
      that.ajax({
        url: "/wechat/index/userReg",
        data: userInfo,
        success: function (json) {
          if (json.res === 1) {
            util.addLogs("注册成功")
            wx.setStorageSync('openId', userInfo.openId)
            wx.setStorageSync("userInfo", userInfo)
            wx.setStorageSync('isReg', true)
          }
        }
      })
    }
    return userInfo
  }
})